package com.example.syssoft.service;


import com.example.syssoft.common.service.LogicService;
import com.example.syssoft.config.Result;
import com.example.syssoft.dao.BusinessDao;
import com.example.syssoft.dao.OrderDao;
import com.example.syssoft.domain.Business;
import com.example.syssoft.domain.Order;
import com.example.syssoft.domain.Shop;
import com.example.syssoft.domain.dto.BusinessDto;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class BusinessService extends LogicService<BusinessDao, Business,Long> {

    @Resource
    BusinessDao businessDao;

    public BusinessService(@Autowired BusinessDao lr) {
        super(lr);
    }


    public Result addShop(Long businessId,Shop shop){
        Optional<Business> byId = businessDao.findById(businessId);
        Business business = byId.get();
        shop.setBusiness(business);
        if (business.addShop(shop)){
            this.POST(business);
            return new Result(true,"添加成功");
        }
        return new Result(false,null,"添加失败");
    }

    public Result getAllBusiness(int size, int page){
        Pageable pageable = PageRequest.of(page,size, Sort.by("id").descending());
        Page<Business> all = getDAO().findAll(pageable);
        List<Business> content = all.getContent();
        List<BusinessDto> newList = new ArrayList<>();

        for (int i = 0; i <content.size() ; i++) {
            BusinessDto businessDto = new BusinessDto();
            BeanUtils.copyProperties(content.get(i),businessDto);
            newList.add(businessDto);
        }

        return new Result(true,newList);
    }


}
